package com.wan.controller;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wan.entity.GameHero;
import com.wan.entity.GameRecord;
import com.wan.entity.GameRole;
import com.wan.entity.OfficeBuild;
import com.wan.service.GameHeroService;
import com.wan.service.GameRecordService;
import com.wan.service.GameRoleService;
import com.wan.service.system.OfficeBuildingService;
import entity.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 办公楼信息表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2020-03-11
 */
@Controller
@RequestMapping("/map/officeBuild")
public class OfficeBuildController {

    private static final long PAGE_SIZE = 10;

    @Autowired
    private OfficeBuildingService officeBuildingService;
    @Autowired
    private GameRecordService recordService;
    @Autowired
    private GameRoleService gameRoleService;
    @Autowired
    private GameHeroService gameHeroService;

    @RequestMapping("/file/upload")
    @ResponseBody
    public ResultBean officeFileUpload(MultipartFile file) {
        ResultBean resultBean = officeBuildingService.fileUpload(file);
        return resultBean;
    }


    @RequestMapping("/save")
    public ModelAndView officeBuildSave(GameRecord record) {
        String str = officeBuildingService.officeBuildSave(record);
        ModelAndView modelAndView = new ModelAndView("uploadoffice");
        List<GameRole> roleList = gameRoleService.list();
        List<GameHero> gameHeroes = gameHeroService.list();
        String gameStartTime = DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss");
        modelAndView.addObject("roleList",roleList);
        modelAndView.addObject("gameHeroes",gameHeroes);
        modelAndView.addObject("gameStartTime",gameStartTime);
        return modelAndView;
    }


    @RequestMapping("/search/list")
    @ResponseBody
    public ResultBean searchList(Long currPage,String title){
        IPage<OfficeBuild> page = new Page<>(currPage,PAGE_SIZE);
        QueryWrapper<OfficeBuild> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("office_building_name",title);
        queryWrapper.orderByDesc("update_time");
        IPage<OfficeBuild> officeBuildIPage = officeBuildingService.page(page,queryWrapper);
        List<OfficeBuild> pageRecords = officeBuildIPage.getRecords();
        Long count = officeBuildIPage.getTotal();
        long totalPage  = (count + PAGE_SIZE -1) / PAGE_SIZE;
        return new ResultBean(0,"成功",totalPage,pageRecords);
    }

    @RequestMapping("/edit")
    @ResponseBody
    public ResultBean updateOffice(Integer id,String officeBuildName,String imageFile,String detiledDescription,String anchor,Integer collageId) {
        ResultBean resultBean = officeBuildingService.updateOffice(id,officeBuildName,imageFile,detiledDescription,anchor,collageId);
        return resultBean;
    }



    @RequestMapping("/delete")
    @ResponseBody
    public ResultBean delete(Integer officeId){
        Boolean b = recordService.removeById(officeId);
        if (b) {
            return new ResultBean(200,"删除成功");
        }else {
            return new ResultBean(500,"删除失败");
        }
    }

    @RequestMapping("/delete/batch")
    @ResponseBody
    public ResultBean deleteBatch(String officeIds){
        String[] strings = officeIds.split(",");
        for (String id : strings){
            Boolean b = recordService.removeById(id);
        }
        return new ResultBean(200,"删除成功");
    }
}

